﻿
namespace FacebookXmpp.Parse
{
	internal class XmlCloseElement : XmlParseState
	{
		public XmlCloseElement( XmlParseState xmlElement )
			: base( xmlElement )
		{ }

		public override XmlParseState Parse(StringParser parser)
		{
            if (parser.PauseIfNearEnd(2))
                return this;

			if (parser.IsNext( "/>" ))
			{
				parser.Consume( "/>" );
			}
			else
			{
				parser.Consume( "</" );
				int close = parser.Find( ">" );
                if (close == -1)
                {
                    parser.Pause();
                    return this;
                }

				parser.ConsumeTo( close + 1 );
			}

			_builder.CloseElement();

			if (_builder.HasOpenElement)
				return new XmlElementInner( this );

			return new XmlStartElement( this );
		}		
	}
}